package com.lhsy.djcloud.controller;


import cn.hutool.crypto.SecureUtil;
import com.lhsy.djcloud.common.ReturnT;
import com.lhsy.djcloud.common.SecurityConstant;
import com.lhsy.djcloud.common.UUIDTools;
import com.lhsy.djcloud.controller.vo.TenantVo;
import com.lhsy.djcloud.entity.Department;
import com.lhsy.djcloud.entity.Tenant;
import com.lhsy.djcloud.entity.User;
import com.lhsy.djcloud.service.DepartmentService;
import com.lhsy.djcloud.service.TenantService;
import com.lhsy.djcloud.service.UserService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import java.util.Date;
import java.util.Objects;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author wangzw
 * @since 2020-09-14
 */
@RestController
@RequestMapping("/tenant")
@Api(value = "Server-API",tags = "注册接口")
public class TenantController  extends BaseController{
    @Autowired
    private TenantService tenantService;
    @Autowired
    private DepartmentService departmentService;
    @Autowired
    private UserService userService;


    @ApiOperation(value = "注册")
    @RequestMapping(value = "/register", method = RequestMethod.POST)
    public ReturnT add(@RequestBody TenantVo tenantVo) {
        //校验用户名称
        User exUser = userService.selectByUserName(tenantVo.getSuperName());
        if (Objects.nonNull(exUser)){
            return ReturnT.FAIL(ReturnT.FAIL_CODE,"用户名称已经存在");
        }

        //创建租户
        Tenant tenant = new Tenant();
        tenant.setTenantName(tenantVo.getTenantName());
        tenant.setAllName(tenantVo.getAllName());
        tenant.setEmail(tenantVo.getEmail());
        tenant.setPhone(tenantVo.getPhone());
        tenant.setLinkName(tenantVo.getLinkName());
        tenant.setRemark(tenantVo.getDesc());
        tenant.setCreateTime(new Date());
        tenantService.save(tenant);

        //创建最顶级的部门
        Department department = new Department();
        department.setTenantId(tenant.getId());
        department.setDeptName(tenant.getAllName());
        department.setDeptCode("D"+tenant.getId()+ UUIDTools.getNonce_str(7));
        department.setLevel(1);
        department.setFid(-1);
        department.setCreateTime(new Date());
        department.setCreateName(tenantVo.getSuperName());
        departmentService.save(department);

        //创建超级管理员
        User user = new User();
        user.setEnabled(Boolean.TRUE);
        user.setTenantId(tenant.getId());
        user.setUsername(tenantVo.getSuperName());
        user.setPassword(SecureUtil.md5(tenantVo.getSuperPwd()));
        user.setDeptId(department.getId());
        user.setRoleName(SecurityConstant.DJG_ROLE);
        userService.save(user);
        return ReturnT.SUCCESS;
    }

}

